package com.ajing.logextractor.consumer;

import ch.qos.logback.classic.LoggerContext;
import com.ajing.logextractor.config.CommonHandlerConfig;
import com.ajing.logextractor.config.LogbackConfig;
import com.ajing.logextractor.config.YamlConfigLoader;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class Main {

    public static void main(String[] args) {
        LogbackConfig.initLogbackConfig();
        if (args.length == 1) {
            // 此处为参数解析测试
            List<CommonHandlerConfig> handlerConfigs = YamlConfigLoader.getConfig().getHandlers();
            LoggerContext loggerContext = new LoggerContext();
            List<LogMessageHandler> logMessageHandlers;
            if (null != handlerConfigs && !handlerConfigs.isEmpty()) {
                logMessageHandlers = handlerConfigs.stream().map((config) -> new LogMessageHandler(config, loggerContext, true)).collect(Collectors.toList());
            } else {
                throw new RuntimeException("未配置消费处理器");
            }
            List<String> recordList = new ArrayList<>(1);
            recordList.add(args[0]);
            for (LogMessageHandler handler : logMessageHandlers) {
                handler.handle(recordList);
            }
        } else {
            LogConsumer.consume();
        }
    }

}
